package cn.rs.lvt.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 用户令牌实体类
 * 存储用户认证令牌及通知配置信息
 */
@Data
@Entity
@Table(name = "rs_access_token", uniqueConstraints = {
        @UniqueConstraint(name = "uk_access_tk_userid_", columnNames = {"uid"}),
        @UniqueConstraint(name = "uk_access_tk_username_", columnNames = {"uname"}),
})
@Comment("访问令牌表：记录用户的访问token和通知配置")
@DynamicUpdate
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AccessTokenEntity {

    /**
     * 主键, 不用自增, 自行指定
     */
    @Id
    @Column(name = "id", length = 64, nullable = false, unique = true)
    @Comment("主键ID：唯一标识记录")
    private String id;

    /**
     * 用户ID
     */
    @Column(name = "uid", nullable = false, unique = true, length = 64)
    @Comment("用户ID")
    private String uid;

    /**
     * 用户名
     */
    @Column(name = "uname", nullable = false, unique = true, length = 64)
    @Comment("用户名")
    private String uname;

    /**
     * 认证令牌
     */
    @Column(name = "token", nullable = false, length = 4096)
    @Comment("认证令牌")
    private String token;

    /**
     * 回调钩子
     */
    @Column(name = "webhook", nullable = false, length = 1024)
    @Comment("回调钩子")
    private String webhook;

    /**
     * 创建时间，由数据库自动记录
     */
    @Column(name = "create_time", nullable = false, updatable = false)
    @Comment("创建时间：记录创建时的时间戳，由数据库自动生成")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @CreationTimestamp
    private Date createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @Comment("更新时间：记录最后更新时的时间戳，由数据库自动更新")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @UpdateTimestamp
    private Date updateTime;

    /**
     * 用户实体
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "uid", referencedColumnName = "id", insertable = false, updatable = false)
    private UserEntity userEntity;

}

